<template>
  <j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk"
           @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="订单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select
                v-model="value1"
                style="width: 100%"
                @change="handleChange"
                :disabled="bianji"
              >
                <a-select-option value="1">根据生产订单创建</a-select-option>
                <a-select-option value="2">根据销售订单创建</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="value1 === '2'">
            <a-form-item label="销售单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup v-decorator="['saleorderCode',validatorRules.saleorderCode]" :trigger-change="true" org-fields="id,order_code,order_name"
                       dest-fields="saleorderId,saleorderCode,saleorderName" code="saleOrder_search" @callback="popupCallback" />
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="value1 === '2'">
            <a-form-item label="销售单名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['saleorderName',validatorRules.saleorderName]" placeholder="请输入销售订单名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="订单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['orderCode',validatorRules.orderCode]" placeholder="请输入订单号"></a-input>
            </a-form-item>
          </a-col>
          
          <!--          <a-col :span="12">-->
          <!--            <a-form-item label="产品型号" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--              <j-dict-select-tag type="list" v-decorator="['orderType']" :trigger-change="true" dictCode="order_type"-->
          <!--                                 placeholder="请选择产品型号" />-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <a-col :span="12" v-if="value1 === '1'">
            <a-form-item label="成品料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup v-decorator="['materielCode',validatorRules.materielCode]" :trigger-change="true" org-fields="id,materiel_code,product_name,gauge,unit,query2"
                       dest-fields="materielId,materielCode,materielName,gauge,unit,orderName" code="product_search" @callback="popupCallback" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="客户料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['orderName',validatorRules.orderName]" placeholder="请输入客户料号" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="value1 === '1'">
            <a-form-item label="成品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['materielName']" placeholder="请输入物料名称" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="value1 === '1'">
            <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['gauge']" placeholder="请输入规格" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="value1 === '1'">
            <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['unit']" placeholder="请输入单位" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="value1 === '1'">
            <a-form-item label="总数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['grossAccount',validatorRules.grossAccount]" placeholder="请输入总数量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="value1 === '2'">
            <a-form-item label="总数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['grossAccount']" placeholder="请输入总数量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="生产阶别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-multi-select-tag type="list_multi" v-decorator="['produceGrade',validatorRules.produceGrade]"
                                  :trigger-change="true" dictCode="produce_grade" placeholder="请选择生产阶别" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="线别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup v-decorator="['lineSort',validatorRules.lineSort]" :trigger-change="true" org-fields="id,productline_name"
                       dest-fields="lineId,lineSort" code="productline_search" @callback="popupCallback" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="上料提醒" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['glazeRemind']" :trigger-change="true"
                                 dictCode="glaze_remind" placeholder="请选择上料提醒" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--<a-input v-decorator="['client']" placeholder="请输入客户"></a-input>-->
              <j-popup
                v-decorator="['client',validatorRules.client]"
                :trigger-change="true"
                org-fields="client_name"
                dest-fields="client"
                code="client_search"
                @callback="popupCallback"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="库存地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup v-decorator="['storageSite',validatorRules.storageSite]" :trigger-change="true" org-fields="id,fac_code,fac_nameone,store_name"
                       dest-fields="factoryId,factoryCode,factoryName,storageSite" code="factorysite_search" @callback="popupCallback" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="工厂编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['factoryCode']" placeholder="请输入工厂编号" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="工厂名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['factoryName']" placeholder="请输入工厂名称" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="计划开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择计划开始日期" v-decorator="['planStart',validatorRules.planStart]" :trigger-change="true" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="计划结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择计划结束日期" v-decorator="['planEnd',validatorRules.planEnd]" :trigger-change="true" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="确认开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择确认开始日期" v-decorator="['confirmStart']" :trigger-change="true" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="确认结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择确认结束日期" v-decorator="['confirmEnd']" :trigger-change="true" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-item>
          </a-col>
          <!-- <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['state']" placeholder="请输入状态"></a-input>
        </a-form-item> -->
          <!-- <a-form-item label="已交货" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['haveDelivery']" placeholder="请输入已交货"></a-input>
				</a-form-item>
				<a-form-item label="基本结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择基本结束日期" v-decorator="['baseEnd']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
					 style="width: 100%" />
				</a-form-item>
				<a-form-item label="计划结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择计划结束日期" v-decorator="['planEnd']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
					 style="width: 100%" />
				</a-form-item>
				<a-form-item label="确认结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择确认结束日期" v-decorator="['confirmEnd']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
					 style="width: 100%" />
				</a-form-item>
				<a-form-item label="基本开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择基本开始日期" v-decorator="['baseStart']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
					 style="width: 100%" />
				</a-form-item>
				<a-form-item label="计划开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择计划开始日期" v-decorator="['planStart']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
					 style="width: 100%" />
				</a-form-item>
				<a-form-item label="确认开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择确认开始日期" v-decorator="['confirmStart']" :trigger-change="true" :show-time="true"
					 date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
				</a-form-item>
				<a-form-item label="计划下达日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择计划下达日期" v-decorator="['planDownreach']" :trigger-change="true" :show-time="true"
					 date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
				</a-form-item>
				<a-form-item label="确认下达日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择确认下达日期" v-decorator="['confirmDownreach']" :trigger-change="true" :show-time="true"
					 date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
				</a-form-item>
				<a-form-item label="MRP管理员编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['mrpadminCode']" placeholder="请输入MRP管理员编号"></a-input>
				</a-form-item>
				<a-form-item label="MRP管理员名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['mrpadminName']" placeholder="请输入MRP管理员名称"></a-input>
				</a-form-item>
				<a-form-item label="生产管理员编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['proadminCode']" placeholder="请输入生产管理员编号"></a-input>
				</a-form-item>
				<a-form-item label="生产管理员名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['proadminName']" placeholder="请输入生产管理员名称"></a-input>
				</a-form-item> -->
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol1" :wrapperCol="wrapperCol1">
              <a-textarea v-decorator="['notes']" rows="4" placeholder="请输入备注" />
            </a-form-item>
          </a-col>
          <a-form-item v-show="false" label="物料ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-show="false" v-decorator="['materielId']" placeholder="请输入物料ID"></a-input>
          </a-form-item>
          <a-form-item v-show="false" label="销售订单ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-show="false" v-decorator="['saleorderId']" placeholder="请输入销售订单ID"></a-input>
          </a-form-item>
        </a-row>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import {
  httpAction
} from '@/api/manage'
import pick from 'lodash.pick'
import {
  validateDuplicateValue
} from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from "@/components/dict/JDictSelectTag"
import JMultiSelectTag from "@/components/dict/JMultiSelectTag"

export default {
  name: "MesOrderProduceModal",
  components: {
    JDate,
    JDictSelectTag,
    JMultiSelectTag,
  },
  data() {
    return {
      value1:'1',
      bianji:true,
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: {
          span: 24
        },
        sm: {
          span: 8
        },
      },
      wrapperCol: {
        xs: {
          span: 24
        },
        sm: {
          span: 16
        },
      },
      labelCol1: {
        xs: {
          span: 24
        },
        sm: {
          span: 4
        },
      },
      wrapperCol1: {
        xs: {
          span: 24
        },
        sm: {
          span: 20
        },
      },


      confirmLoading: false,
      validatorRules: {
        client: {
          rules: [{
            required: true,
            message: '请选择客户!'
          }, ]
        },
        saleorderCode: {
          rules: [{
            required: true,
            message: '请输入销售单号!'
          }, ]
        },
        saleorderName: {
          rules: [{
            required: true,
            message: '请输入销售单名称!'
          }, ]
        },
        orderCode: {
          rules: [{
            required: true,
            message: '请输入订单编号!'
          }, ]
        },
        /* orderName: {
          rules: [{
            required: true,
            message: '请输入订单名称!'
          }, ]
        }, */
        materielCode:{
          rules: [{
            required: true,
            message: '请输入成品料号!'
          }, ]
        },
        grossAccount:{
          rules: [{
            required: true,
            message: '请输入总数量!'
          }, ]
        },
        lineSort: {
          rules: [{
            required: true,
            message: '线别不能为空!'
          }, ]
        },
        storageSite:{
          rules: [{
            required: true,
            message: '库存地点不能为空!'
          }, ]
        },
        produceGrade: {
          rules: [{
            required: true,
            message: '生产阶别不能为空!'
          }, ]
        },
        glazeRemind: {
          rules: [{
            required: true,
            message: '上料提醒不能为空!'
          }, ]
        },
        planStart:{
          rules: [{
            required: true,
            message: '计划开始时间不能为空!'
          }, ]
        },
        planEnd:{
          rules: [{
            required: true,
            message: '计划结束时间不能为空!'
          }, ]
        },

      },
      url: {
        add: "/order/mesOrderProduce/add",
        edit: "/order/mesOrderProduce/edit",
      },
      recordId:true,
    }
  },
  created() {},
  methods: {
    handleChange(){
      this.form.resetFields();
      if (!this.recordId) {
        this.form.setFieldsValue({glazeRemind:'10%'})
      }
    },
    add() {
      this.bianji = false;
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'createBy', 'createTime', 'orderCode', 'orderName', 'orderType',
          'materielCode', 'materielName', 'gauge', 'factoryCode', 'factoryName', 'storageSite', 'state',
          'grossAccount',
          'unit', 'lineSort', 'produceGrade', 'glazeRemind', 'client', 'haveDelivery', 'baseEnd', 'planEnd',
          'confirmEnd', 'baseStart', 'planStart',
          'confirmStart',
          'planDownreach', 'confirmDownreach', 'saleorderCode', 'saleorderName', 'mrpadminCode',
          'mrpadminName',
          'proadminCode', 'proadminName', 'notes', 'materielId', 'saleorderId'))
        if (!record.id) {
          this.recordId = false
          this.form.setFieldsValue({glazeRemind:'10%'})
        }
        if (record) {
          if (record.saleorderCode || record.saleorderName){
            this.value1 = '2'
          }else{
            this.value1 = '1'
          }
        }
      })
    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
              that.close();
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;

          })
        }

      })
      // location.reload()
      // this.$router.go(0)
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'createBy', 'createTime', 'orderCode', 'orderName', 'orderType',
        'materielCode',
        'materielName', 'gauge', 'factoryCode', 'factoryName', 'storageSite', 'state', 'grossAccount', 'unit',
        'lineSort', 'produceGrade', 'glazeRemind', 'client',
        'haveDelivery', 'baseEnd', 'planEnd', 'confirmEnd', 'baseStart', 'planStart', 'confirmStart',
        'planDownreach',
        'confirmDownreach', 'saleorderCode', 'saleorderName', 'mrpadminCode', 'mrpadminName', 'proadminCode',
        'proadminName', 'notes', 'materielId', 'saleorderId'))
    },


  }
}
</script>
